//
//  CourseForecastViewController.m
//  ECBBusinessCamp
//
//  Created by rimi on 16/6/16.
//  Copyright © 2016年 邓毕华. All rights reserved.
//

#import "CourseForecastViewController.h"



@interface CourseForecastViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSArray *arr;
@property (nonatomic,strong) CourseForecastDetailViewController *courseForecastDetailVc;
@property (nonatomic,strong) CourseForecastSharesViewController *shareVc;


@end

@implementation CourseForecastViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.tableView.rowHeight = 380;
    [self loadData];
    
    
}

#pragma mark -- loadData --
#pragma mark --
- (void)loadData{
    
    NSMutableDictionary * parameters = [NSMutableDictionary dictionaryWithObjects:@[LoginUserInfo.defaultUserInfo.token] forKeys:@[@"token"]];

    [[NetTool sharedTool] requestMethod:POST loadingMsg:@"加载中..." url:BASEURLAPPEND(CLASSFORECAST_URL) parameters:parameters finish:^(id responseObject, NSError *error) {
        
        [self loadDataSuccess:responseObject];
        _arr = responseObject[@"data"];
        [MBProgressHUD showSuccess:@"网络请求成功"];
        //更新视图
        
        [self.tableView reloadData];
      
    }];
}

#pragma mark -- 数据处理 --
#pragma mark --
- (void)loadDataSuccess:(NSDictionary *)result{
    _arr = result[@"data"];
    
}

#pragma mark -- UITableViewDataSource UITableViewDelegate--
#pragma mark --

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   return _arr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CourseForecastTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"CourseForecastTableViewCell"];
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (!cell) {
        cell = [[CourseForecastTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CourseForecastTableViewCell"];
        
    }
    NSString *url =  BASEURLAPPEND(_arr[indexPath.row][@"pic"]);
    [NSURL URLWithString:url];
    [cell.imagev sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"user_default"]];
    cell.title.text = _arr[indexPath.row][@"className"];
    cell.actor.text =  [NSString stringWithFormat:@"%@%@",@"讲师:",_arr[indexPath.row][@"teacherName"]];
    cell.time.text = [NSString stringWithFormat:@"%@%@",@"时间:",[NSDate dateWithTimeIntervalSince1970:[_arr[indexPath.row][@"startTime"][@"time"] doubleValue]/1000]];
    cell.adress.text = [NSString stringWithFormat:@"%@%@",@"地点:",_arr[indexPath.row][@"place"]];
    cell.forecast.text = [NSString stringWithFormat:@"%@%@",@"简介:",_arr[indexPath.row][@"desce"]];
    
    NSInteger totalNum = [_arr[indexPath.row][@"totalNum"] integerValue];
    NSInteger currentNum = [_arr[indexPath.row][@"currentNum"] integerValue];
    cell.numberOfPerson.text = [NSString stringWithFormat:@"已有%ld人报名，剩余%ld个名额",currentNum,(totalNum - currentNum)];
    cell.classId = _arr[indexPath.row][@"id"];
    
    //分享按钮的回调
    [cell clickShare:^{
        _shareVc = [self.storyboard instantiateViewControllerWithIdentifier:@"CourseForecastSharesViewController"];
        [self addChildViewController:_shareVc];
        [self.view addSubview:_shareVc.view];
        NSLog(@"加载分享vc");
    }];
   
    return cell;
    
}
#pragma mark -- cell的block回调 --
#pragma mark --

-(void)CellBtnBlock{
    
    [self addChildViewController:_shareVc];
    [self.view addSubview:_shareVc.view];
}

#pragma mark --详情页--
#pragma mark --

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    _courseForecastDetailVc = [self.storyboard instantiateViewControllerWithIdentifier:@"CourseForecastDetailViewController"];
    _courseForecastDetailVc.detailDicSource = _arr[indexPath.row];//初始化时就付值，然后再添加视图，得到数据
    _courseForecastDetailVc.view.center = CGPointMake(self.view.center.x * 3, self.view.center.y);
    [self animationz:_courseForecastDetailVc.view];//动画
    
    [self addChildViewController:_courseForecastDetailVc];
    [self.view addSubview:_courseForecastDetailVc.view];
    NSLog(@"点击%ld",(long)indexPath.row);
    
}



#pragma mark -- 详情页动画 --
#pragma mark --
- (void)animationz:(UIView *)someoneview{
    
    [UIView animateWithDuration:1 animations:^{
          someoneview.center = self.view.center;
    } completion:nil];
    
  
}



@end
